home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / mach / hurd / __fcntl.c < prev    next >
C/C++ Source or Header  |  1994-05-21  |  5KB  |  189 lines

  1. /* Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <fcntl.h>
  22. #include <hurd.h>
  23. #include <hurd/fd.h>
  24. #include <stdarg.h>
  25.  
  26.  
  27. /* Perform file control operations on FD.  */
  28. int
  29. DEFUN(__fcntl, (fd, cmd), int fd AND int cmd DOTS)
  30. {
  31.   va_list ap;
  32.   struct hurd_fd *d;
  33.   int result;
  34.   
  35.   HURD_CRITICAL_BEGIN;
  36.  
  37.   d = _hurd_fd_get (fd);
  38.  
  39.   if (d == NULL)
  40.     {
  41.       result = __hurd_fail (EBADF);
  42.       goto out;
  43.     }
  44.  
  45.   va_start (ap, cmd);
  46.  
  47.   switch (cmd)
  48.     {
  49.     default:            /* Bad command.  */
  50.       __spin_unlock (&d->port.lock);
  51.       errno = EINVAL;
  52.       result = -1;
  53.       break;
  54.  
  55.       /* First the descriptor-based commands, which do no RPCs.  */
  56.  
  57.     case F_DUPFD:        /* Duplicate the file descriptor.  */
  58.       {
  59.     struct hurd_fd *new;
  60.     io_t port, ctty;
  61.     struct hurd_userlink ulink, ctty_ulink;
  62.     int flags;
  63.  
  64.     /* Extract the ports and flags from the file descriptor.  */
  65.     flags = d->flags;
  66.     ctty = _hurd_port_get (&d->ctty, &ctty_ulink);
  67.     port = _hurd_port_locked_get (&d->port, &ulink); /* Unlocks D.  */
  68.  
  69.     /* Get a new file descriptor.  The third argument to __fcntl is the
  70.        minimum file descriptor number for it.  */
  71.     new = _hurd_alloc_fd (&result, va_arg (ap, int));
  72.     if (new == NULL)
  73.       /* _hurd_alloc_fd has set errno.  */
  74.       result = -1;
  75.     else
  76.       {
  77.         /* Give the ports each a user ref for the new descriptor.  */
  78.         __mach_port_mod_refs (__mach_task_self (), port,
  79.                   MACH_PORT_RIGHT_SEND, 1);
  80.         if (ctty != MACH_PORT_NULL)
  81.           __mach_port_mod_refs (__mach_task_self (), ctty,
  82.                     MACH_PORT_RIGHT_SEND, 1);
  83.  
  84.         /* Install the ports and flags in the new descriptor.  */
  85.         if (ctty != MACH_PORT_NULL)
  86.           _hurd_port_set (&new->ctty, ctty);
  87.         _hurd_port_locked_set (&new->port, port);
  88.         /* Duplication clears the FD_CLOEXEC flag.  */
  89.         new->flags = flags & ~FD_CLOEXEC;
  90.       }
  91.  
  92.     _hurd_port_free (&d->port, &ulink, port);
  93.     if (ctty != MACH_PORT_NULL)
  94.       _hurd_port_free (&d->ctty, &ctty_ulink, port);
  95.  
  96.     break;
  97.       }
  98.  
  99.     case F_GETFD:        /* Get descriptor flags.  */
  100.       result = d->flags;
  101.       __spin_unlock (&d->port.lock);
  102.       break;
  103.  
  104.     case F_SETFD:        /* Set descriptor flags.  */
  105.       d->flags = va_arg (ap, int);
  106.       __spin_unlock (&d->port.lock);
  107.       result = 0;
  108.       break;
  109.  
  110.  
  111.       /* Now the real io operations, done by RPCs to io servers.  */
  112.  
  113.     case F_GETLK:
  114.     case F_SETLK:
  115.     case F_SETLKW:
  116.       {
  117.     struct flock *fl = va_arg (ap, struct flock *);
  118.     __spin_unlock (&d->port.lock);
  119.     errno = fl?ENOSYS:EINVAL; /* XXX mib needs to implement io rpcs.  */
  120.     result = -1;
  121.     break;
  122.       }
  123.  
  124.     case F_GETFL:        /* Get per-open flags.  */
  125.       {
  126.     struct hurd_userlink ulink;
  127.     io_t port
  128.       = _hurd_port_locked_get (&d->port, &ulink); /* Unlocks D.  */ 
  129.     error_t err;
  130.  
  131.     err = __io_get_openmodes (port, &result);
  132.  
  133.     _hurd_port_free (&d->port, &ulink, port);
  134.  
  135.     if (err)
  136.       result = __hurd_fail (err);
  137.     break;
  138.       }
  139.  
  140.     case F_SETFL:        /* Set per-open flags.  */
  141.       {
  142.     const int flags = va_arg (ap, int);
  143.     struct hurd_userlink ulink;
  144.     io_t port
  145.       = _hurd_port_locked_get (&d->port, &ulink); /* Unlocks D.  */
  146.     error_t err;
  147.  
  148.     err = __io_set_all_openmodes (port, flags);
  149.         
  150.     _hurd_port_free (&d->port, &ulink, port);
  151.  
  152.     result = err ? __hurd_fail (err) : 0;
  153.     break;
  154.       }
  155.  
  156.     case F_GETOWN:        /* Get owner.  */
  157.       {
  158.     struct hurd_userlink ulink;
  159.     error_t err;
  160.     io_t port
  161.       = _hurd_port_locked_get (&d->port, &ulink); /* Unlocks D.  */
  162.     err = __io_get_owner (port, &result);
  163.     _hurd_port_free (&d->port, &ulink, port);
  164.     result = err ? __hurd_fail (err) : 0;
  165.     break;
  166.       }
  167.  
  168.     case F_SETOWN:        /* Set owner.  */
  169.       {
  170.     pid_t owner = va_arg (ap, pid_t);
  171.     error_t err;
  172.     struct hurd_userlink ulink;
  173.     io_t port
  174.       = _hurd_port_locked_get (&d->port, &ulink); /* Unlocks D.  */
  175.     err = __io_mod_owner (port, owner);
  176.     _hurd_port_free (&d->port, &ulink, port);
  177.     result = err ? __hurd_fail (err) : 0;
  178.     break;
  179.       }
  180.     }
  181.  
  182.   va_end (ap);
  183.  
  184.  out:
  185.   HURD_CRITICAL_END;
  186.  
  187.   return result;
  188. }
  189.